%Paul Ozog
%HW 3
%Noise/Stoch

%The approach will be as follows:
%
%
%
%     +-----------+        +------------+
%     | Rand      |        |  Subtract  |
%     | Generator |--------|     .5     |--
%     |           |        |            |  \--
%     +-----------+        +------------+     \- +------------+
%                                               \+     	      |	output
%                          	       		 |   Adder    +------
%              	       	   		         /            |
%                  			       /-+------------+
%     					     /-
%     +-----------+        +------------+  /-
%     | Rand      |        |  Subtract  |/-
%     | Generator |--------|     .5     /
%     |           |        |            |
%     +-----------+        +------------+
%
%

%1. Generate two *distinct* sets of random numbers
%   between 0 and 1
%2. Shift each left by 0.5
%3. Add the two sets. This will have a histogram as
%   shown in the problem.

%Ten million gives a nice strait line
N = 10000000;

%steps 1. and 2.
y1 = rand(1,N) - 0.5;
y2 = rand(1,N) - 0.5;

%step 3.
out = y1 + y2;

%Plot a histogram of the inputs and output
outbins = -1:.01:1;
y1bins = -0.5:.01:0.5;
y2bins = -0.5:.01:0.5;

outHist = hist(out, outbins) / N * 100;
y1Hist  = hist(y1,  y1bins)  / N * 100;
y2Hist  = hist(y2,  y2bins)  / N * 100;

subplot(3,1,1);
plot(y1bins, y1Hist, 'r');
title('pdf of Y1');
xlabel('x');
axis([-1 1 0 2]);

subplot(3,1,2);
plot(y2bins, y2Hist, 'g');
title('pdf of Y2');
axis([-1 1 0 2]);

subplot(3,1,3);
plot(outbins, outHist);
title('pdf of the Output');
